package cn.lili.controller.supplier;

import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.client.GoodsClient;
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
import cn.lili.modules.supplier.entity.dto.SupplierGoodsOperationDTO;
import cn.lili.modules.supplier.service.SupplierGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 供应商,商品接口
 *
 * @author pikachu
 * @since 2020-02-23 15:18:56
 */
@RestController
@Api(tags = "供应商端,商品接口")
@RequestMapping("/supplier/goods")
@RequiredArgsConstructor
public class SupplierGoodsController {

    private final SupplierGoodsService supplierGoodsService;

    private final GoodsClient goodsClient;

    @ApiOperation(value = "新增商品")
    @PostMapping(value = "/create")
    public ResultMessage<GoodsOperationDTO> save(@Valid @RequestBody SupplierGoodsOperationDTO supplierGoodsOperationDTO) {

        //参数自检
        supplierGoodsOperationDTO.paramsCheck();

        supplierGoodsService.addGoods(supplierGoodsOperationDTO);
        return ResultUtil.success();
    }

    @ApiOperation(value = "修改商品")
    @PutMapping(value = "/update/{goodsId}")
    public ResultMessage<GoodsOperationDTO> update(@RequestBody SupplierGoodsOperationDTO supplierGoodsOperationDTO, @PathVariable String goodsId) {


        supplierGoodsOperationDTO.paramsCheck();

        supplierGoodsOperationDTO.setGoodsId(goodsId);
        supplierGoodsService.editGoods(supplierGoodsOperationDTO);

        //todo 修改其他代发商品即订单问题处理
        return ResultUtil.success();
    }

    /**
            * 修改商品分类
     * @return
             */
    @ApiOperation(value = "修改商品")
    @PostMapping(value = "/update/ids")
    public ResultMessage<GoodsOperationDTO> updateCategory(@RequestBody SupplierGoodsOperationDTO supplierGoodsOperationDTO) {
        if (Objects.requireNonNull(UserContext.getCurrentUser()).getScene().equals(SceneEnums.SUPPLIER) || Objects.requireNonNull(UserContext.getCurrentUser()).getScene().equals(SceneEnums.MANAGER)) {
            supplierGoodsService.editGoodsCategory(supplierGoodsOperationDTO);
            //goodsIntegrationService.updateGoods(goodsOperationDTO.getGoodsIds(), goodsOperationDTO.getCategoryPathIds());
            return ResultUtil.success();
        } else {
            return ResultUtil.error();
        }
    }

    /**
     * 供应商，批量添加分类
     */
    @PostMapping("/add/categoryPathSon")
    public ResultMessage<Object> addCategoryPathSon(@RequestParam String ids, @RequestParam String categoryPathSon, @RequestParam String categoryPathSonName) {
        return supplierGoodsService.addCategoryPathSon(ids, categoryPathSon, categoryPathSonName);
    }

    @ApiOperation(value = "获取批发规则")
    @GetMapping(value = "/wholesale/{goodsId}")
    @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "query", allowMultiple = true)
    public ResultMessage<Object> deleteGoods(@PathVariable String goodsId) {
        return ResultUtil.data(goodsClient.getWholesale(goodsId));
    }

    @ApiOperation(value = "删除商品")
    @DeleteMapping(value = "/delete")
    @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "query", allowMultiple = true)
    public ResultMessage<Object> deleteGoods(@RequestParam List<String> goodsId) {

        supplierGoodsService.deleteGoods(goodsId);
        //todo 删除其他代发商品即订单问题处理
        return ResultUtil.success();
    }

}
